package com.cjkj.equipment.controller;

import com.cjkj.common.core.domain.R;
import com.cjkj.common.core.utils.bean.BeanUtils;
import com.cjkj.equipment.domain.MonitorPadData;
import com.cjkj.equipment.domain.SmartBraceletData;
import com.cjkj.equipment.service.MonitorPadDataService;
import com.cjkj.equipment.service.PatientSleepStageRecordService;
import com.cjkj.equipment.service.SmartBraceletDataService;
import com.cjkj.equipment.service.TJ03DataService;
import com.cjkj.system.api.domain.*;
import com.cjkj.system.api.domain.request.RecordsReq;
import com.cjkj.system.api.domain.request.SleepStageReq;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/report")
public class ReportController {

    @Resource
    private SmartBraceletDataService smartBraceletDataService;

    @Resource
    private PatientSleepStageRecordService recordService;

    @Resource
    private MonitorPadDataService monitorPadDataService;

    @Resource
    private TJ03DataService tj03DataService;

    @PostMapping("/tj02Data")
    public R<SmartBraceletRecords> listSmartBraceletRecords(@RequestBody RecordsReq req){
        SmartBraceletRecords smartBraceletRecords = new SmartBraceletRecords();
        List<SmartBraceletData> smartBraceletDataList = smartBraceletDataService.listData(req.getEquipmentNo(), req.getStartTime(), req.getEndTime());
        List<APISmartBraceletData> apiSmartBraceletDataList = new ArrayList<>();
        smartBraceletDataList.forEach(e -> {
            APISmartBraceletData apiSmartBraceletData = new APISmartBraceletData();
            BeanUtils.copyProperties(e,apiSmartBraceletData);
            apiSmartBraceletDataList.add(apiSmartBraceletData);
        });
        smartBraceletRecords.setDataList(apiSmartBraceletDataList);
        return R.ok(smartBraceletRecords);
    }

    @PostMapping("/tj01StageRecords")
    public R<SleepStageRecords> tj01StageRecords(@RequestBody SleepStageReq sleepStageReq){
        SleepStageRecords records = new SleepStageRecords();
        List<PatientSleepStageRecord> patientSleepStageRecords = recordService.listRecords(sleepStageReq.getEquipmentNo(),
                sleepStageReq.getStartTime(),
                sleepStageReq.getEndTime());
        records.setSleepStageRecordList(patientSleepStageRecords);
        return R.ok(records);
    }

    @PostMapping("/tj01Data")
    public R<MonitorPadRecords> tj01Data(@RequestBody RecordsReq req){
        MonitorPadRecords monitorPadRecords = new MonitorPadRecords();
        List<MonitorPadData> monitorPadData = monitorPadDataService.listData(req);
        List<APIMonitorPadData> apiMonitorPadDataList = new ArrayList<>();
        monitorPadData.forEach(e -> {
            APIMonitorPadData apiMonitorPadData = new APIMonitorPadData();
            BeanUtils.copyProperties(e,apiMonitorPadData);
            apiMonitorPadDataList.add(apiMonitorPadData);
        });
        monitorPadRecords.setMonitorPadDataList(apiMonitorPadDataList);
        return R.ok(monitorPadRecords);
    }

    @PostMapping("/tj03HrData")
    public R<List<APITJ03HrData>> tj03HrData(@RequestBody RecordsReq req){
        return R.ok(tj03DataService.listHrData(req.getEquipmentNo(), req.getStartTime(), req.getEndTime()));
    }

    @PostMapping("/tj03BpData")
    public R<List<APITJ03BpData>> tj03BpData(@RequestBody RecordsReq req){
        return R.ok(tj03DataService.listBpData(req.getEquipmentNo(), req.getStartTime(), req.getEndTime()));
    }

    @PostMapping("/tj03ECGData")
    public R<List<APITJ03EcgData>> tj03ECGData(@RequestBody RecordsReq req){
        return R.ok(tj03DataService.listECGData(req.getEquipmentNo(), req.getStartTime(), req.getEndTime()));
    }

    @PostMapping("/tj03LatestHrData")
    public R<APITJ03HrData> tj03LatestHrData(@RequestBody RecordsReq req){
        return R.ok(tj03DataService.latestHrData(req.getEquipmentNo(), req.getStartTime(), req.getEndTime()));
    }

    @PostMapping("/getTj03RealTimeData")
    public R<APITJ03RealTimeData> getTj03RealTimeData(@RequestBody List<RecordsReq> reqs){
        return R.ok(tj03DataService.getTj03RealTimeData(reqs));
    }
}
